package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.depot.DepotOperator;
import com.scisoft.scan.data.depot.DepotOrderDetailBean;
import com.scisoft.scan.data.depot.DepotOrderStatis;
import com.scisoft.scan.data.depot.DepotQueryBean;
import com.scisoft.scan.data.enterdepot.EnterDepotOperator;
import com.scisoft.scan.data.enterdepot.EnterDepotOrderBean;
import com.scisoft.scan.data.enterdepot.EnterDepotOrderDetailBean;
import com.scisoft.scan.data.enterdepot.EnterDepotOrderStatis;
import com.scisoft.scan.data.pack.PackDetailBean;
import com.scisoft.scan.data.shelves.ShelvesInfoBean;
import com.scisoft.scan.data.stock.StockInfoBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_EnterDepot {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/EnterDepotServer?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://enterDepot.scan.ws.dms.com/";


    //总库入库订单统计(orderStatus 1 全部2未处理3处理中4已完成  condition 查询条件)
    public static EnterDepotOrderStatis ENTER_01(String orderStatus,String condition) throws Exception {
//        EnterDepotOrderStatis ret = new EnterDepotOrderStatis();
//        ret.allOrderCount = 100;
//        ret.undoOrderCount = 200;
//        ret.doneOrderCount = 80;
//        ret.doingOrderCount = 125;
//        ret.pageCount = 10;//一定要注意：这个页数要根据orderStatus配合使用，例如当orderStatus=1时候，总数就=allOrderCount的数量。例如当orderStatus=2时，总数就=undoOrderCount；以此类推。
//        ret.pageSize = 10;
//        return ret;
        if ("".equals(condition)||condition == null){
            condition = "@@";
        }
        String bizId = orderStatus+","+condition;
//        EnterDepotOrderStatis parm = new EnterDepotOrderStatis(orderStatus,condition);
        EnterDepotOrderStatis ret = new EnterDepotOrderStatis();
//        String arg0 = String.format("{\"flowNo\":\"ENTER_01\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence);
        String arg0 = String.format( "{\"flowNo\":\"ENTER_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user,SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(),new TypeToken<EnterDepotOrderStatis>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询总库入库订单(orderStatus 1 全部2未处理3处理中4已完成  condition 查询条件)
    public static List<EnterDepotOrderBean> ENTER_02(String orderStatus,String condition,int pagesize, int pageindex) throws Exception {

//        List<EnterDepotOrderBean> ret = new ArrayList<>();
//        EnterDepotOrderBean e = new EnterDepotOrderBean();
//        e.orderId = "111111";
//        e.operator = "张三";
//        e.optime = "2021年12月11日";
//        e.orderNo = "222222";
//        e.orderState = "处理中";
//        ret.add(e);
//        return ret;
        if ("".equals(condition)||condition == null){
            condition = "@@";
        }
        String bizId = orderStatus+","+condition;
        List<EnterDepotOrderBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"ENTER_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<EnterDepotOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个总库入库订单的详细信息（orderId 订单编号， dtlId 明细ID）
    public static List<EnterDepotOrderDetailBean> ENTER_03(String orderId) throws Exception {
//        List<EnterDepotOrderDetailBean> ret = new ArrayList<>();
//        EnterDepotOrderDetailBean e = new EnterDepotOrderDetailBean();
//        e.partName = "玻璃";
//        e.partNo = "111111";
//        e.supplierCode = "大运";
//        e.supplierName = "大运";
//        e.partCode = "P00001";
//        e.partCount = "200";
//        e.notPickedCount = "200";
//        e.pickedCount = "0";
//        e.ifOk = "0";
//        ret.add(e);
//        return ret;
        String arg0=String.format( "{\"flowNo\":\"ENTER_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        List<EnterDepotOrderDetailBean> ret = new ArrayList<>();
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<EnterDepotOrderDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //单个明细完成（dtlId 明细ID，数量是全部，通过左滑方式提交的）
    public static String ENTER_04(String dtlId) throws Exception {
//        return "成功";
        EnterDepotOrderDetailBean bean =new EnterDepotOrderDetailBean(dtlId);
        Gson gson = new Gson();
        String parm =gson.toJson(bean);
        String arg0 = String.format( "{\"flowNo\":\"ENTER_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //单个明细处理取消完成（dtlId 明细ID）
    public static String ENTER_05(String dtlId) throws Exception {
//        return "成功";
        EnterDepotOrderDetailBean bean =new EnterDepotOrderDetailBean(dtlId);
        Gson gson = new Gson();
        String parm =gson.toJson(bean);
        String arg0 = String.format( "{\"flowNo\":\"ENTER_05\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //单个订单全部处理完成提交 （orderId 订单编号）
    public static String ENTER_06(String orderId) throws Exception {
//        return "成功";
        String arg0 = String.format( "{\"flowNo\":\"ENTER_06\",\"account\":\"%s\",\"bizId\":%s,\"scSequence\":\"%s\"}",SysParameter.user,orderId, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //单个明细拣货提交（数量可能不是全部，而是手工输入）
    public static String ENTER_07(String orderId,EnterDepotOrderDetailBean enterDepotOrderDetailBean) throws Exception {
//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(enterDepotOrderDetailBean);
        String arg0 = String.format( "{\"flowNo\":\"ENTER_07\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //获取当前订单的最后的操作员
    public static EnterDepotOperator ENTER_09(String orderId) throws Exception {

//        EnterDepotOperator ret = new EnterDepotOperator();
//        ret.operatorName = "张三";
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"ENTER_09\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        EnterDepotOperator ret = new EnterDepotOperator();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<EnterDepotOperator>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //强制更新当前订单的最后的操作员
    public static String ENTER_10(String orderId, EnterDepotOperator depotOperator) throws Exception {
        String bizId= orderId+","+depotOperator.operatorName;
        String arg0 = String.format( "{\"flowNo\":\"ENTER_10\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
